/**
 * @file   SendTTF001File.java
 * @date   2012-11-12下午11:18:36
 * @author User
 */
package com.gzceb.zhgjj.mina.TTF001;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gzceb.zhgjj.mina.MinaConstants;
import com.gzceb.zhgjj.model.TTF001File;
import com.gzceb.zhgjj.util.PropertiesUtils;


/**
 * <p>TTF001发送反馈文件类。</p>
 *
 * @date   2012-11-12下午11:18:36 
 * @author liqing
 * @description 
 */
public class SendTTF001File  extends IoHandlerAdapter {
	private  final Logger logger = LoggerFactory.getLogger(getClass());
	
	private String fileName;
	private TTF001File tTF001File;
	
	public String getFileName(){
		return this.fileName;
	}
	public SendTTF001File(String fileName){
		super();
		this.fileName = fileName;
	}
	@Override  
    public void exceptionCaught(IoSession arg0, Throwable arg1)   
            throws Exception {   
        // TODO Auto-generated method stub   
		
		arg0.close(true);
    }   
  
    /**  
     * 当客户端接受到消息时  
     */  
    @Override  
    public void messageReceived(IoSession session, Object message) throws Exception {   
  
        //我们已设定了服务器的消息规则是一行一行读取，这里就可以转为String:   
        String s = (String)message;   
           
        //Writer the received data back to remote peer   
        logger.info("服务器发来的收到消息: " + s);   
        String rspStr = tTF001File.ProcRspFile(s); 
        if( MinaConstants.F_NAK.getValue().equals(rspStr)){
        	session.close(true);
        }else{
        	//测试将消息回送给客户端   
        	session.write(rspStr);
        }
  
    }   
  
    @Override  
    public void messageSent(IoSession arg0, Object arg1) throws Exception {   
        // TODO Auto-generated method stub   
    	super.messageSent(arg0, arg1);
    }   
  
    /**  
     * 当一个客户端被关闭时  
     */  
    @Override  
    public void sessionClosed(IoSession session) throws Exception {   
        logger.info("one client Disconnect"); 
        super.sessionClosed(session);
  
    }   
  
    @Override  
    public void sessionCreated(IoSession session) throws Exception {   
        // TODO Auto-generated method stub   
    	tTF001File = new TTF001File(PropertiesUtils.getValue("rspPath"));
    	tTF001File.setFileName(fileName);
    	session.write(MinaConstants.F_NAME.getValue() + fileName); 
    }   
  
    @Override  
    public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {   
        // TODO Auto-generated method stub
    	logger.debug("关闭闲置session，ip：" + arg0.getRemoteAddress().toString());
    	arg0.close(false);
  
    }   
  
    /**  
     * 当一个客户端连接进入时  
     */  
    @Override  
    public void sessionOpened(IoSession session) throws Exception {   
  
        logger.info("incomming client:" + session.getRemoteAddress());   
          
  
    }   

}
